package gaml.text;

import gaml.error.GamlException;

import java.util.ArrayList;


/**
 * system-registered yaml text parsers lives here
 * @author shnireck
 *
 */
public class YamlParserStore {
//	LinkedHashMap <Pattern, YamlTextParser> parsers = new LinkedHashMap <Pattern, YamlTextParser> ();
	ArrayList <YamlTextParser> parsers = new ArrayList <YamlTextParser> ();
	YamlTextParser defaultParser = null;
	
	/**
	 * new parsers store should get default parser
	 * @param defaultParser
	 */
	public YamlParserStore (YamlTextParser defaultParser) { this.defaultParser = defaultParser; register (defaultParser); }
	
	/**
	 * add new parser
	 * @param newParser parser to add
	 * @return host object
	 */
	public YamlParserStore register (YamlTextParser newParser) { parsers.add (newParser); return this; }
	
	/**
	 * check if we have a parser to parse some text string
	 * @param text yaml text to be parsed
	 * @return true if propriate parser found, false if not
	 * @throws GamlException 
	 */
	public boolean hasParser (YamlText text) throws GamlException {
		for (YamlTextParser parser: parsers)
			if (parser.isParserFor (text)) return true;
		return false;
	}
	
	/**
	 * get specific parser for yaml text
	 * @param match
	 * @return
	 * @throws GamlException
	 */
	public YamlTextParser getParser (YamlText text) throws GamlException {
		for (YamlTextParser parser: parsers)
			if (parser.isParserFor (text)) return parser;
		throw new GamlException (String.format ("Parser not found for node text %s", text.getValue ()));
	}
	
	/**
	 * default yaml text parser
	 * @return parser, set as default for store
	 */
	public YamlTextParser getDefaultParser () { return defaultParser; }
}
